package com.fingard.rh.fgmsp.transmanage.biz.query.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.fingard.rh.fgmsp.transmanage.biz.query.FingardTransactionsService;
import com.fingard.rh.fgmsp.transcommon.dto.transaction.FingardTransactionsDTO;
import com.fingard.rh.fgmsp.transcommon.dto.transaction.FingardTransactionsPO;
import com.fingard.rh.fgmsp.transmanage.manager.query.FingardTransactionsManager;
import com.fingard.rh.fgmsp.transmanage.dto.query.FingardTransactionsParam;
import com.fingard.rh.fgmsp.transmanage.dto.query.CountsDTO;
import com.github.pagehelper.PageInfo;
import com.fingard.rh.fgmsp.common.bean.PageView;
import com.fingard.rh.fgmsp.common.bean.ResultContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author lincc
 * @version 1.0 2019/03/29
 */
@Slf4j
@Service
public class FingardTransactionsServiceImpl implements FingardTransactionsService {

    @Autowired
    private FingardTransactionsManager fingardTransactionsManager;

    @Override
    public PageView<FingardTransactionsDTO> listByParam(FingardTransactionsParam fingardTransactionsParam) {
        PageInfo<FingardTransactionsDTO> pageInfo = fingardTransactionsManager.listByParam(fingardTransactionsParam);
        PageView<FingardTransactionsDTO> pageView = new PageView<>();
        pageView.setRows(this.translate(pageInfo.getList()));
        pageView.setTotal(pageInfo.getTotal());
        return pageView;
    }

    @Override
    public ResultContext<CountsDTO> countByParam(FingardTransactionsParam fingardTransactionsParam){
        CountsDTO countsDTO = fingardTransactionsManager.countByParam(fingardTransactionsParam);
        return new ResultContext<>(countsDTO);
    }

    @Override
    public ResultContext<FingardTransactionsPO> getById(String entNum, String id) {
        FingardTransactionsPO fingardTransactionsPO = fingardTransactionsManager.queryByPrimaryKey(entNum, id);
        return new ResultContext<>(fingardTransactionsPO);
    }

    private <T> List<T> translate(List<T> list) {
        if (CollectionUtil.isNotEmpty(list)) {
            if(list.get(0) instanceof FingardTransactionsDTO){
                for (T dto: list) {
                    FingardTransactionsDTO fingardTransactionsDTO = (FingardTransactionsDTO) dto;
                    this.translate(fingardTransactionsDTO);
                }
            }
        }
        return list;
    }

    private FingardTransactionsDTO translate(FingardTransactionsDTO fingardTransactionsDTO) {
       return fingardTransactionsDTO;
    }
}
